<?php

namespace Home\Controller;
use Think\Controller;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class HomeController extends Controller {

	/* 空操作，用于输出404页面 */
	public function _empty(){
		$this->redirect('Index/index');
	}


    protected function _initialize(){
        /* 读取站点配置, 修改common下是否有configlists文件, 再查询数据库config表数据 */

//        $file   = is_file($config)? $config : $path.'Conf/'.$config.CONF_EXT;
//        if(is_file($file)) {
//            is_numeric($key)?C(load_config($file)):C($key,load_config($file));
//        }
        $config = api('Config/lists');
        C($config); //添加配置

        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }
        # 导航
        $navList=M('channel')->where("status=%d",array('status'=>1))->order('sort asc, id asc')->select();
        $navList = D('Common/Tree')->toTree($navList);
        $this->assign('navList', $navList);
        # END-导航
        # 友情链接
        $friendLinkList = M('FriendLink')->where("status=%d",array('status'=>1))->order('sort asc, id asc')->select();
        $this->assign('friendLinkList', $friendLinkList);
        # END-友情链接
    }

	/* 用户登录检测 */
	protected function login(){
		/* 用户登录检测 */
		is_login() || $this->error('您还没有登录，请先登录！', U('User/login'));
	}


    /**
     * 获取分类树所有分类id
     * @param $cate
     * @param string $returnType
     * @return array|string
     */
    protected function getTreeToCateIdStr($cate, $returnType="string")
    {
        $result = array();
        $cateList = tree_to_list(array($cate));
        foreach($cateList as $cate){
            $result[] = $cate['id'];
        }
        switch($returnType){
            case 'string':
                return implode(',', $result);
            default:
                return $result;
        }
    }


    /**
     * 通用分页列表数据集获取方法
     *
     *  可以通过url参数传递where条件,例如:  index.html?name=asdfasdfasdfddds
     *  可以通过url空值排序字段和方式,例如: index.html?_field=id&_order=asc
     *  可以通过url参数r指定每页数据条数,例如: index.html?r=5
     *
     * @param sting|Model  $model   模型名或模型实例
     * @param array        $where   where查询条件(优先级: $where>$_REQUEST>模型设定)
     * @param array|string $order   排序条件,传入null时使用sql默认排序或模型属性(优先级最高);
     *                              请求参数中如果指定了_order和_field则据此排序(优先级第二);
     *                              否则使用$order参数(如果$order参数,且模型也没有设定过order,则取主键降序);
     *
     * @param boolean      $field   单表模型用不到该参数,要用在多表join时为field()方法指定参数
     * @author 朱亚杰 <xcoolcc@gmail.com>
     *
     * @return array|false
     * 返回数据集
     */
    protected function lists ($model,$where=array(),$order='',$field=true, $group=null){
        $options    =   array();
        $REQUEST    =   (array)I('request.');
        if(is_string($model)){
            $model  =   M($model);
        }

        $OPT        =   new \ReflectionProperty($model,'options');
        $OPT->setAccessible(true);

        $pk         =   $model->getPk();
        $pk         =   is_array($pk) && count($pk) > 0 ? current($pk) : $pk;
        if($order===null){
            //order置空
        }else if ( isset($REQUEST['orderDirection']) && isset($REQUEST['orderField']) && in_array(strtolower($REQUEST['orderDirection']),array('desc','asc')) ) {
            $options['order'] = '`'.$REQUEST['orderField'].'` '.$REQUEST['orderDirection'];
        }elseif( $order==='' && empty($options['order']) && !empty($pk) ){
            $options['order'] = $pk.' desc';
        }elseif($order){
            $options['order'] = $order;
        }
        unset($REQUEST['orderDirection'],$REQUEST['orderField']);

        if(empty($where)){
            $where  =   array('status'=>array('egt',0));
        }
        if( !empty($where)){
            $options['where']   =   $where;
        }
        $options      =   array_merge( (array)$OPT->getValue($model), $options );
        $total        =   $model->where($options['where'])->count();

        if( isset($REQUEST['pageSize']) ){
            $listRows = (int)$REQUEST['pageSize'];
        }else{
            $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        }
        $page = new \Think\Page($total, $listRows, $REQUEST);
        if(defined('CUSTOM_PAGE') && CUSTOM_PAGE){
            if($total>$listRows){
                $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            }
            $p =$page->show();
            $this->assign('_page', $p? $p: '');
        }
        $pageCurrent = $REQUEST['pageCurrent'] > 0 ? (int)$REQUEST['pageCurrent'] : 1;
        $this->assign('pageSize', $listRows);
        $this->assign('pageCurrent', $pageCurrent);
        $this->assign('total',$total);
        $options['limit'] = $page->firstRow.','.$page->listRows;
        $model->setProperty('options',$options);

        return $model->field($field)->group($group)->select();
    }


}
